#include "WeChatLogin.h"
#include "json/rapidjson.h"
#include "json/document.h"
#include "network/HttpRequest.h"
#include "network/HttpClient.h"
#include "network/HttpResponse.h"
#include "sdk/message/Common.h"
#include "sdk/ui/PromptLayer.h"
#include "sdk/util/Util.h"

USING_NS_CC;

#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
#include "WeChatLoginAndroid.h"
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
#include "WeChatLoginIOS.h"
#endif

#ifndef HEAD_IMG_
#define HEAD_IMG_	"WECHAT_HEAD_IMG"
#endif

#ifndef ACCESS_TIME_OUT_EX
#define ACCESS_TIME_OUT_EX		2*60*60
#endif

#ifndef REFRESH_TIME_OUT_EX
#define REFRESH_TIME_OUT_EX		30*24*60*60
#endif

#ifndef REFRESH_REFRESH_TIME_EX
#define REFRESH_REFRESH_TIME_EX	28*24*60*60
#endif

WeChatLogin *WeChatLogin::_instance = nullptr;

WeChatLogin* WeChatLogin::getInstance()
{
	if(_instance == nullptr)
	{
		_instance = new WeChatLogin();
		_instance->retain();
	}
	return _instance;
}

WeChatLogin::WeChatLogin()
{
	memset(&_userInfo,0,sizeof(_userInfo));
	_tokenCallback		= nullptr;
	_userInfoCallback	= nullptr;
	_isRequesting		= false;
	_shareCallback		= nullptr;
	_dwUserId			= 0;
	_tokenCallback		= nullptr;
	_headImgCallback	= nullptr;	//这个函数已经放到全局函数，不需要清空
	_shareCallback		= nullptr;
}

WeChatLogin::~WeChatLogin()
{

}

void WeChatLogin::reset()
{
	_accessToken		= "";
	_refreshToken		= "";
	_unionId			= "";
	_openId				= "";
}

bool WeChatLogin::isInstalled()
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
    return isInstanlledAndroid();
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
	return isInstalledIOS();
#endif
	return false;
}

void WeChatLogin::setCode(const char* code)
{
	_code = code;
	WH_LogErr("WeChatLogin",StringUtils::format(U2F("set Code, _code=%s").c_str(), _code.c_str()).c_str());
}

void WeChatLogin::setAppId(const char* appId)
{
	_appId = appId;
	WH_LogErr("WeChatLogin",StringUtils::format(U2F("set AppId, _appId=%s").c_str(), _appId.c_str()).c_str());
}

void WeChatLogin::setSecret(const char* secret)
{
	_appSecret = secret;
	WH_LogErr("WeChatLogin",StringUtils::format(U2F("set Secret, _appSecret=%s").c_str(), _appSecret.c_str()).c_str());
}

void WeChatLogin::auth(const wechatHttpRequestCallback &callBack)
{
	_tokenCallback = nullptr;
	_tokenCallback = callBack;
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
	_appId				= getAppID();
	_appSecret			= getAppSecret();
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
	_appId				= getAppID();
	_appSecret			= getAppSecret();
#endif
	_accessToken		=  GetFunc("AccessToken","",String);
	_refreshToken		= GetFunc("RefreshToken","",String);
	_unionId			= GetFunc("UnionID","",String);
	_openId				= GetFunc("OpenID","",String);
	time_t accessTime	= GetFunc("RequestAccessTime",0,Double);
	time_t refreshTime	= GetFunc("RequestRefreshTime",0,Double);
	time_t now			= time(NULL);

	WH_LogErr("WeChatLogin","_auth");
	WH_LogErr("WeChatLogin",StringUtils::format("_auth accessTime=%d, refreshTime=%d, now=%d", now - accessTime, now - refreshTime).c_str());

	if(now - accessTime < ACCESS_TIME_OUT_EX)
	{
		WH_LogErr("WeChatLogin","access token is useable ,login direct");
		getAccessTokenBack(1);
	}
	else
	{
		if(now - refreshTime < REFRESH_TIME_OUT_EX)
		{
			WH_LogErr("WeChatLogin","refresh token is going to out of time,refresh the refresh token");
			refreshAccessToken([this](int code, string msg)
			{
				if(code != 0)
				{
					PromptLayer::createObject(msg.c_str());
					authStart(1);
					return;
				}
				getAccessTokenBack(2);
			});
		}
		else
		{
			WH_LogErr("WeChatLogin","refresh token has out of time,or you have not doing the first auth,start auth");
			authStart(2);
		}
	}
}

void WeChatLogin::clearCallBack()
{
	_tokenCallback		= nullptr;
	_shareCallback		= nullptr;
}

bool WeChatLogin::checkCanAuth()
{
	return isInstalled();
}

WeChatUserInfo WeChatLogin::getUserInfo()
{
	return _userInfo;
}

void WeChatLogin::requestAccessToken()
{
	if(_isRequesting)
	{
	//	return;
	}
	_isRequesting = true;
	WH_LogErr("WeChatLogin", "request Access Token");
	reset();
	
	network::HttpRequest* pRequest = new network::HttpRequest();
	std::string strUrl = StringUtils::format("https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code", _appId.c_str(), _appSecret.c_str(), _code.c_str());
	
	WH_LogErr("WeChatLogin",StringUtils::format(U2F("get access token strUrl = %s").c_str(),strUrl.c_str()).c_str());
	pRequest->setUrl(strUrl.c_str());
	pRequest->setRequestType(network::HttpRequest::Type::GET);

	std::vector<std::string> headers;
	headers.push_back("Content-Type: application/json;charset=utf-8");
	pRequest->setHeaders(headers);
	pRequest->setTag("GET_ACCESS_TOKEN");
	pRequest->setResponseCallback([this](network::HttpClient* pClient, network::HttpResponse* pResponse)
	{
		WH_LogErr("WeChatLogin", "request Access Token callback");
		if(!pResponse->isSucceed()){
			_isRequesting = false;
			if(_tokenCallback){
				_tokenCallback(2,U2F("请求失败"));
			}
			return;
		}
		string jsonStr="";
		for(int i = 0;i< pResponse->getResponseData()->size();i++)
		{
			jsonStr.append(StringUtils::format("%c",pResponse->getResponseData()->at(i)));
		}
		WH_LogErr("AccessToken",StringUtils::format("%s",jsonStr.c_str()).c_str());
		rapidjson::Document jsonData;
		jsonData.Parse<0>(jsonStr.c_str());
		if(jsonData.HasParseError())
		{
			_isRequesting = false;
			if(_tokenCallback){
				_tokenCallback(1,U2F("json 解析失败"));
			}
			return;
		}

		if(jsonData.HasMember("errcode"))
		{
			_isRequesting = false;
			rapidjson::Value &err = jsonData["errcode"];
			rapidjson::Value &msg = jsonData["errmsg"];
			if(_tokenCallback){
				_tokenCallback(err.GetInt(),msg.GetString());
			}
			return;
		}

		if(jsonData.HasMember("access_token"))
		{
			const rapidjson::Value& accessToken = jsonData["access_token"];
			_accessToken = accessToken.GetString();
			WH_LogErr("WeChatLogin",StringUtils::format("request Access Token, access_token=%s", accessToken.GetString()).c_str());
		}

		if(jsonData.HasMember("openid"))
		{
			const rapidjson::Value& openId = jsonData["openid"];
			_openId = openId.GetString();
			WH_LogErr("WeChatLogin",StringUtils::format("request Access Token, openid=%s", openId.GetString()).c_str());
		}

		if(jsonData.HasMember("refresh_token"))
		{
			const rapidjson::Value& refreshToken = jsonData["refresh_token"];
			_refreshToken = refreshToken.GetString();
			WH_LogErr("WeChatLogin",StringUtils::format("request Access Token, refresh_token=%s", refreshToken.GetString()).c_str());
		}

		if(jsonData.HasMember("unionid"))
		{
			const rapidjson::Value& unionId = jsonData["unionid"];
			_unionId = unionId.GetString();
			WH_LogErr("WeChatLogin",StringUtils::format("request Access Token, unionid=%s", unionId.GetString()).c_str());
		}

		if(jsonData.HasMember("expires_in"))
		{
			const rapidjson::Value& expire = jsonData["expires_in"];
			_expires = expire.GetInt();
			WH_LogErr("WeChatLogin",StringUtils::format("request Access Token, expires_in=%d", expire.GetInt()).c_str());
		}
		_isRequesting	= false;
		SaveFunc("AccessToken", _accessToken, String);
		SaveFunc("RefreshToken", _refreshToken, String);
		SaveFunc("UnionID", _unionId, String);
		SaveFunc("OpenID", _openId, String);
		time_t now		= time(NULL);
		SaveFunc("RequestAccessTime", now, Double);
		SaveFunc("RequestRefreshTime", now, Double);

		getAccessTokenBack(3);
	});
	network::HttpClient::getInstance()->send(pRequest);
	pRequest->release();
}

void WeChatLogin::requestUserInfo()
{
	if(_isRequesting)
	{
	//	return;
	}
	_isRequesting = true;
	WH_LogErr("WeChatLogin", "request User Info");

	network::HttpRequest* pRequest = new network::HttpRequest();
	std::string strUrl = StringUtils::format("https://api.weixin.qq.com/sns/userinfo?access_token=%s&openid=%s",_accessToken.c_str(),_openId.c_str());
	
	WH_LogErr("WeChatLogin",StringUtils::format(U2F("get userinfo strUrl = %s").c_str(),strUrl.c_str()).c_str());
	
	pRequest->setUrl(strUrl.c_str());
	pRequest->setRequestType(network::HttpRequest::Type::GET);

	std::vector<std::string> headers;
	headers.push_back("Content-Type: application/json;charset=utf-8");
	pRequest->setHeaders(headers);
	pRequest->setTag("GET_USERINFO");

	pRequest->setResponseCallback([this](network::HttpClient* pClient, network::HttpResponse* pResponse)
	{
		if(!pResponse->isSucceed()){
			_isRequesting = false;
			if(_tokenCallback){
				_tokenCallback(2,U2F("请求失败"));
			}
			return;
		}
		string jsonStr="";
		for(int i = 0;i< pResponse->getResponseData()->size();i++){
			if(pResponse->getResponseData()->at(i) == '\\'){
				continue;
			}
			jsonStr.append(StringUtils::format("%c", pResponse->getResponseData()->at(i)));
		}
		WH_LogErr("WeChatLogin",StringUtils::format("UserInfo=%s",jsonStr.c_str()).c_str());
		rapidjson::Document jsonData;
		jsonData.Parse<0>(jsonStr.c_str());
		WH_LogErr("UserInfo",U2F("get UserInfo ok").c_str());
		if(jsonData.HasParseError())
		{
			_isRequesting = false;
			if(_tokenCallback){
				_tokenCallback(1,U2F("json err"));
			}
			return;
		}

		if(jsonData.HasMember("errcode"))
		{
			_isRequesting = false;
			rapidjson::Value &err = jsonData["errcode"];
			rapidjson::Value &msg = jsonData["errmsg"];
			if(_tokenCallback){
				_tokenCallback(err.GetInt(),msg.GetString());
			}
			return;
		}

		if(jsonData.HasMember("sex"))
		{
			rapidjson::Value &value = jsonData["sex"];
			_sex = value.GetInt();
			WH_LogErr("WeChatLogin",StringUtils::format("request User Info, sex=%d",value.GetInt()).c_str());
		}

		if(jsonData.HasMember("nickname"))
		{
			rapidjson::Value &value = jsonData["nickname"];
			_nick = value.GetString();
			WH_LogErr("WeChatLogin",StringUtils::format("request User Info, nickname=%s",value.GetString()).c_str());
		}

		if(jsonData.HasMember("country"))
		{
			rapidjson::Value &value = jsonData["country"];
			_country = value.GetString();
			WH_LogErr("WeChatLogin",StringUtils::format("request User Info, country=%s",value.GetString()).c_str());
		}

		if(jsonData.HasMember("province"))
		{
			rapidjson::Value &value = jsonData["province"];
			_province = value.GetString();
			WH_LogErr("WeChatLogin",StringUtils::format("request User Info, province=%s",value.GetString()).c_str());
		}

		if(jsonData.HasMember("openid"))
		{
			rapidjson::Value &value = jsonData["openid"];
			_openId = value.GetString();
			WH_LogErr("WeChatLogin",StringUtils::format("request User Info, openid=%s",value.GetString()).c_str());
		}

		if(jsonData.HasMember("headimgurl"))
		{
			rapidjson::Value &value = jsonData["headimgurl"];
			_headUrl = value.GetString();
			WH_LogErr("WeChatLogin",StringUtils::format("request User Info, headimgurl=%s",value.GetString()).c_str());
		}

		if(jsonData.HasMember("unionid"))
		{
			rapidjson::Value &value = jsonData["unionid"];
			_unionId = value.GetString();
			WH_LogErr("WeChatLogin",StringUtils::format("request User Info, unionid=%s",value.GetString()).c_str());
		}

		//----add by tony.tian
		if(jsonData.HasMember("language"))
		{
			rapidjson::Value &value = jsonData["language"];
			string _language = value.GetString();
			WH_LogErr("WeChatLogin",StringUtils::format("request User Info, language=%s",value.GetString()).c_str());
		}

		if(jsonData.HasMember("city"))
		{
			rapidjson::Value &value = jsonData["city"];
			string _city = value.GetString();
			WH_LogErr("WeChatLogin",StringUtils::format("request User Info, city=%s",value.GetString()).c_str());
		}

		if(jsonData.HasMember("unionid"))
		{
			rapidjson::Value &value = jsonData["unionid"];
			string _unionid = value.GetString();
			WH_LogErr("WeChatLogin",StringUtils::format("request User Info, unionid=%s",value.GetString()).c_str());
		}

		WH_LogErr("WeChatLogin",U2F("get user info end").c_str());
		_isRequesting = false;
		if(_tokenCallback){
			_tokenCallback(0,"");
		}
	});
	network::HttpClient::getInstance()->send(pRequest);
	pRequest->release();
}

void WeChatLogin::getAccessTokenBack(int index)
{
	WH_LogErr("WeChatLogin",StringUtils::format(U2F(" get Access Token Back index=%d").c_str(), index).c_str());
	requestUserInfo();
}

void WeChatLogin::verifyAccessTokenUseable(const wechatHttpRequestCallback &callBack)
{
	WH_LogErr("WeChatLogin", "verifyAccessToken");
	_verifyCakkback = callBack;
	network::HttpRequest* pRequest = new network::HttpRequest();
	std::string strUrl = StringUtils::format("https://api.weixin.qq.com/sns/auth?access_token=%s&openid=%s",_accessToken.c_str(),_openId.c_str());

	WH_LogErr("WeChatLogin",StringUtils::format(U2F(" verify access token strUrl = %s").c_str(),strUrl.c_str()).c_str());
	pRequest->setUrl(strUrl.c_str());
	pRequest->setRequestType(network::HttpRequest::Type::GET);

	std::vector<std::string> headers;
	headers.push_back("Content-Type: application/json;charset=utf-8");
	pRequest->setHeaders(headers);
	pRequest->setTag("VERIFY_ACCESS_TOKEN");
	pRequest->setResponseCallback([this, callBack](network::HttpClient* pClient, network::HttpResponse* pResponse)
	{
		WH_LogErr("WeChatLogin", " Verify AccessToken callback");
		if(!pResponse->isSucceed())
		{
			_verifyCakkback(2,U2F("request failed"));
			return;
		}
		string jsonStr="";
		for(int i = 0;i< pResponse->getResponseData()->size();i++)
		{
			jsonStr.append(StringUtils::format("%c",pResponse->getResponseData()->at(i)));
		}
		WH_LogErr("AccessToken",StringUtils::format("%s",jsonStr.c_str()).c_str());
		rapidjson::Document jsonData;
		jsonData.Parse<0>(jsonStr.c_str());
		if(jsonData.HasParseError())
		{
			_verifyCakkback(1, U2F("json jiexi err"));
			return;
		}

		if(jsonData.HasMember("errcode"))
		{
			rapidjson::Value &err = jsonData["errcode"];
			rapidjson::Value &msg = jsonData["errmsg"];
			_verifyCakkback(err.GetInt(), msg.GetString());
			return;
		}
	});
	network::HttpClient::getInstance()->send(pRequest);
	pRequest->release();
}

void WeChatLogin::requestHeadImg(const headImgHttpRequestCallback &callBack, unsigned int dwUserId,int size)
{
	_dwUserId			= dwUserId;
	_headImgCallback	= nullptr;
	_headImgCallback	= callBack;
	loadUserHead(size);
}

void WeChatLogin::refreshAccessToken(const wechatHttpRequestCallback &callBack)
{
	WH_LogErr("WeChatLogin", "Refresh Access Token");
	_refreshCallback = callBack;
	network::HttpRequest* pRequest = new network::HttpRequest();
	std::string strUrl = StringUtils::format("https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=%s&grant_type=refresh_token&refresh_token=%s",_appId.c_str(),_refreshToken.c_str());

	WH_LogErr("WeChatLogin",StringUtils::format(U2F(" refresh access token strUrl = %s").c_str(),strUrl.c_str()).c_str());
	pRequest->setUrl(strUrl.c_str());
	pRequest->setRequestType(network::HttpRequest::Type::GET);

	std::vector<std::string> headers;
	headers.push_back("Content-Type: application/json;charset=utf-8");
	pRequest->setHeaders(headers);
	pRequest->setTag("REFRESH_ACCESS_TOKEN");
	pRequest->setResponseCallback([this](network::HttpClient* pClient, network::HttpResponse* pResponse)
	{
		WH_LogErr("WeChatLogin", " Verify AccessToken callback");
		if(!pResponse->isSucceed())
		{
			_refreshCallback(2,U2F("request failed"));
			return;
		}
		string jsonStr="";
		for(int i = 0;i< pResponse->getResponseData()->size();i++)
		{
			jsonStr.append(StringUtils::format("%c",pResponse->getResponseData()->at(i)));
		}
		WH_LogErr("AccessToken",StringUtils::format("%s",jsonStr.c_str()).c_str());
		rapidjson::Document jsonData;
		jsonData.Parse<0>(jsonStr.c_str());
		if(jsonData.HasParseError())
		{
			_refreshCallback(1,U2F("json jiexi failed"));
			return;
		}

		if(jsonData.HasMember("errcode"))
		{
			rapidjson::Value &err = jsonData["errcode"];
			rapidjson::Value &msg = jsonData["errmsg"];
			_refreshCallback(err.GetInt(),msg.GetString());
			return;
		}
		
		if(jsonData.HasMember("refresh_token"))
		{
			const rapidjson::Value& refreshToken = jsonData["refresh_token"];
			_refreshToken = refreshToken.GetString();
			WH_LogErr("WeChatLogin",StringUtils::format(U2F("refresh Access Token, _refreshToken=%s").c_str(), _refreshToken.c_str()).c_str());
		}

		if(jsonData.HasMember("access_token"))
		{
			const rapidjson::Value& accessToken = jsonData["access_token"];
			_accessToken = accessToken.GetString();
			WH_LogErr("WeChatLogin",StringUtils::format(U2F("refresh Access Token, _accessToken=%s").c_str(), _accessToken.c_str()).c_str());
		}

		if(jsonData.HasMember("expires_in"))
		{
			const rapidjson::Value& expire = jsonData["expires_in"];
			_expires = expire.GetInt();
			WH_LogErr("WeChatLogin",StringUtils::format(U2F("refresh Access Token, _expires=%d").c_str(), _expires).c_str());
		}

		if(jsonData.HasMember("openid"))
		{
			const rapidjson::Value& openId = jsonData["openid"];
			_openId = openId.GetString();
			WH_LogErr("WeChatLogin",StringUtils::format(U2F("refresh Access Token, _openId=%s").c_str(), _openId.c_str()).c_str());
		}

		//----add by tony.tian
		if(jsonData.HasMember("scope"))
		{
			const rapidjson::Value& scopeInfo = jsonData["scope"];
			string _scope = scopeInfo.GetString();
			WH_LogErr("WeChatLogin",StringUtils::format(U2F("refresh Access Token, _scope=%s").c_str(), _scope.c_str()).c_str());
		}

		SaveFunc("AccessToken", _accessToken, String);
		SaveFunc("RefreshToken", _refreshToken, String);
		SaveFunc("OpenID", _openId, String);
		time_t now = time(NULL);
		SaveFunc("RequestAccessTime", now, Double);
		SaveFunc("RequestRefreshTime", now, Double);
		_refreshCallback(0, U2F(""));
	});
	network::HttpClient::getInstance()->send(pRequest);
	pRequest->release();
}

void WeChatLogin::loadUserHead(int size)
{
	//_headUrl = "http://wx.qlogo.cn/mmopen/uSBU8sESVvZJ7re4DxdSty8QicVjcJeicdlzaHdsmgPibNLbhBDTOHny83vtBqLuHuziaeLOGOXnw6pvpicRmiaegb44e6ScDbXyqh/0";
	WH_LogErr("WeChatLogin",U2F("request head data").c_str());
	WH_LogErr("WeChatLogin",StringUtils::format(U2F("load User Head, _headUrl=%s").c_str(), _headUrl.c_str()).c_str());
	if(_headUrl.length() <= 0)
	{
		if(_headImgCallback != nullptr)
		{
			_headImgCallback(1,U2F("head url is null..."),nullptr,0);
		}
		return;
	}
	string strUrl = _headUrl.substr(0,_headUrl.length()-1);
	strUrl.append(StringUtils::format("%d",size).c_str());
	WH_LogErr("WeChatLogin",strUrl.c_str());
	WH_LogErr("WeChatLogin",StringUtils::format(U2F("load User Head, size=%d, strUrl=%s").c_str(), size, _headUrl.c_str()).c_str());
	network::HttpRequest* pRequest = new network::HttpRequest();
	//pRequest->setUrl(strUrl.c_str());
	pRequest->setUrl(_headUrl.c_str());
	pRequest->setRequestType(network::HttpRequest::Type::GET);
	pRequest->setTag("LOAD_USER_HEAD");

	pRequest->setResponseCallback([this](network::HttpClient* pClient, network::HttpResponse* pResponse)
	{
		WH_LogErr("WeChatLogin",U2F("get head info http return").c_str());
		if(!pResponse->isSucceed()){
			WH_LogErr("WeChatLogin",pResponse->getErrorBuffer());
			if(_headImgCallback != nullptr){
				_headImgCallback(2, U2F("request err"), nullptr, 0);
			}
			return;
		}
		std::vector<char> *buffer = pResponse->getResponseData();
		Image *img = new Image();
		img->initWithImageData((unsigned char*)buffer->data(),buffer->size());

		string szWritePath = FileUtils::getInstance()->getWritablePath();
		if(!szWritePath.empty())
		{
			FileUtils::getInstance()->addSearchPath(szWritePath);
			string wPath = szWritePath;
			if(szWritePath.find_last_of("/") != szWritePath.length()-1)
			{
				wPath = szWritePath.append("/");
			}
			wPath = wPath.append(StringUtils::toString(WeChatLogin::getInstance()->getUserID())).append(".png");
			remove(wPath.c_str());
			FILE* file = fopen(wPath.c_str(), "wb+");
			if (file) {
				size_t rLen = fwrite(buffer->data(), sizeof(char), buffer->size(), file);
				if(rLen > 0)
				{
					WH_LogErr("WeChatLogin","save headimg to local success");
					NotificationCenter::getInstance()->postNotification(LOAD_WECHAT_USERHEAD);
				}else{
					WH_LogErr("WeChatLogin","save headimg to local fail");
				}

				fclose(file);
			}
		}
		saveImg((unsigned char*)buffer->data(),buffer->size());

		if(_headImgCallback != nullptr)
		{
			_headImgCallback(0,U2F("get head success"),(unsigned char*)buffer->data(),buffer->size());
		}
		NotificationCenter::getInstance()->postNotification(GET_WECHAT_HEAD_IMG);
	});
	network::HttpClient::getInstance()->send(pRequest);
	pRequest->release();
}

void WeChatLogin::authStart(int index)
{
	WH_LogErr("WeChatLogin",StringUtils::format("auth Start index=%d", index).c_str());
	
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
	authAndroid();
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
	if(isInstalled())
	{
		authIOS();
	}
	else
	{
		WH_LogErr("WeChatLogin", "WeChat is not installed!");
	}
#endif
}

void WeChatLogin::wechatShare(string title, string text, int type, const shareCallback& callback, string url/* = ""*/)
{
	_shareCallback	= nullptr;
	_shareCallback	= callback;
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
	onShareSuccess();
#else
	Util::weixinShare(title, text, type, url);
#endif
}

unsigned int WeChatLogin::getUserID()
{
	return _dwUserId;
}

void WeChatLogin::onShareSuccess()
{
	WH_LogErr("WeChatLogin", "on WeChat Share Success callback");
	if(_shareCallback){
		_shareCallback(0);
		_shareCallback	= nullptr;
	}
}

void WeChatLogin::saveImg(unsigned char*buf,int size)
{

}
